#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <stdlib.h>


#define MB_UNIT "MB"
#define KB_UNIT "KB"

struct test_struct {
    char *iqn;
    int fd;
    double fragment_size;
    char *fragment_size_unit;
    char *path;
};
static void parse_config(struct test_struct *test, char* cfgSttring);
static void parse_config_by_array(struct test_struct *test, char* cfgString);
static void print_strcut(struct test_struct *test);

int main()
{
    printf("Hello world\n");
    //Config string format:  Backstorage Type/IQN/Backstorage Name;Fragment Size
    char *defalut_config_string_with_virgule = "Type//iqn.2019-07.com.viptest.tcmu.target/bakcstorage_name/20KB";
    char *defalut_config_string_with_semicolon = "Type//iqn.2019-07.com.viptest.tcmu.target/bakcstorage_name/20KB";
    struct test_struct *state = (struct test_struct *) (calloc(1, sizeof(*state)));
    // parse_config(state, defalut_config_string);
    parse_config_by_array(state, defalut_config_string_with_virgule);
    print_strcut(state);
    system("pause");
    return 0;
}

static void parse_config(struct test_struct *test, char* cfgSttring)
{
    char *first_virgule_symbol = strchr(cfgSttring, '/');
    char *semicolon_symbol = strchr(cfgSttring, ';');
    char *fragment_size_str = semicolon_symbol + 1;
    printf("fragment size string : %s\n", fragment_size_str);
    int fragment_size_length = strlen(fragment_size_str);
    printf("fragment size string length : %d\n", fragment_size_length);
    int file_path_length = semicolon_symbol - first_virgule_symbol - 1;
    char *file_desc_path = (char *) calloc(file_path_length, sizeof(char));
    strncpy(file_desc_path, first_virgule_symbol + 1, file_path_length);
    printf("file desc path : %s\n", file_desc_path);
    char *second_virgule_symbol = strchr(file_desc_path + 1, '/');
    int length = second_virgule_symbol - file_desc_path - 1;
    printf("pointer1-pointer2 = %d \n", length);
    char *config = (char *) calloc(length, sizeof(char));
    strncpy(config, file_desc_path + 1, length);
    test->iqn = strcat(config, "\0");
    test->fragment_size = strtod(fragment_size_str, &test->fragment_size_unit);
    printf("iqn : %s\n", test->iqn);
    printf("fragment size : %lf\n", test->fragment_size);
    printf("fragment size unit : %s\n", test->fragment_size_unit);
}

static void parse_config_by_array(struct test_struct *test, char* cfgString)
{
    char *iqn = (char *) calloc(100, sizeof(char));
    char *lun = (char *) calloc(50, sizeof(char));
    char *frag = (char *) calloc(10, sizeof(char));
    char *path = (char *) calloc(150, sizeof(char));;
    int length, virgule_symbol_count, iqn_size, lun_size, frag_size;
	length = strlen(cfgString);
	virgule_symbol_count = 0;
	iqn_size = 0;
	lun_size = 0;
	frag_size = 0; 
    for (int i = 0; i < length; i++)
    {
        if ('/' == cfgString[i])
        {
            virgule_symbol_count++;
        }

        switch (virgule_symbol_count)
        {
            case 0:
            case 1:
                break;
            case 2:
                iqn[iqn_size++] = cfgString[i];
                break;
            case 3:
                lun[lun_size++] = cfgString[i];
                break;
            case 4:
                frag[frag_size++] = cfgString[i];
            default:
                break;
        }        
    }
    test->fragment_size = strtod(frag+1, &test->fragment_size_unit);
    test->fragment_size = strtod(frag+1, &test->fragment_size_unit);
	if (strcmp(test->fragment_size_unit, MB_UNIT) == 0)
	{
		test->fragment_size *= (1024 * 1024);
	}
	else if (strcmp(test->fragment_size_unit, KB_UNIT) == 0)
	{
		test->fragment_size *=1024;
	}
    test -> iqn = iqn + 1;
    strcat(path, iqn);
    test -> path = strcat(path, lun);
}

static void print_strcut(struct test_struct *test)
{
    printf("iqn : %s\n", test->iqn);
    printf("fragment size : %lf\n", test->fragment_size);
    printf("fragment size unit : %s\n", test->fragment_size_unit);
    printf("path : %s\n", test->path);
}